package com.tsm.picachu.user.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.tsm.picachu.GeneralActivity;
import com.tsm.picachu.R;
import com.tsm.picachu.common.Constant;
import com.tsm.picachu.common.Utils;
import com.tsm.picachu.data.User;
import com.tsm.picachu.mainmenu.ui.MainMenuActivity;

public class SelectUserActivity extends GeneralActivity
{
	private LinearLayout mLayoutListUser = null;
	private String[] mArrayNames = null;
	private String mArrayNamesString = null;

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.page_select_user);
		mLayoutListUser = (LinearLayout) findViewById(R.id.user_list);

		mArrayNamesString = Utils.getPreference(this, Constant.PREFERENCE_NAME,
				Constant.PREFERENCE_KEY_ALL_USERS);

		if (mArrayNamesString == null)
		{
			mArrayNamesString = "";
		}

		if (!TextUtils.isEmpty(mArrayNamesString))
		{
			mArrayNames = mArrayNamesString.split("["
					+ Constant.USER_NAME_SEPARATE + "]");

			fillListUser();
		}

		final EditText editTextName = (EditText) findViewById(R.id.user_input_name);
		final Button buttonOk = (Button) findViewById(R.id.user_input_ok);
		buttonOk.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				String name = editTextName.getText().toString();
				if (TextUtils.isEmpty(name))
				{
					return;
				}

				if (mArrayNamesString.equals(""))
				{
					mArrayNamesString += name;
				}
				else
				{
					mArrayNamesString += Constant.USER_NAME_SEPARATE + name;
				}
				Utils.savePreference(SelectUserActivity.this,
						Constant.PREFERENCE_NAME,
						Constant.PREFERENCE_KEY_ALL_USERS, mArrayNamesString);

				User user = new User(name);
				user.setHuman(Constant.TOTAL_HUMANS_FIRST);
				Utils.saveUser(SelectUserActivity.this, user);

				gotoMainMenu(name);
			}
		});
	};

	private void gotoMainMenu(String name)
	{
		Intent intent = new Intent(SelectUserActivity.this,
				MainMenuActivity.class);
		intent.putExtra(Constant.EXTRA_USERNAME, name);
		startActivity(intent);
	}

	private void fillListUser()
	{
		if (mArrayNames == null || mArrayNames.length == 0)
		{
			return;
		}

		mLayoutListUser.removeAllViews();

		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		for (int indexName = 0; indexName < mArrayNames.length; indexName++)
		{
			View view = layoutInflater.inflate(R.layout.layout_one_user, null);
			final String name = mArrayNames[indexName];

			Button buttonName = (Button) view.findViewById(R.id.layout_one_user_name);
			buttonName.setText(name);
			buttonName.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					gotoMainMenu(name);
				}
			});

			mLayoutListUser.addView(view);
		}
	}
}
